<template>
  <el-dialog :visible="isShow" :modal="false" title="新增操作员信息" @close="closeDialog">
      <el-form ref="form" :model="operator" label-width="80px">
        <el-form-item label="姓名"
        prop="name"
        :rules="[
          { required: true, message: '请输入操作员姓名'}
        ]">
          <el-input class="input-box" v-model="operator.name" placeholder="请输入姓名" size="small"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input class="input-box" v-model="operator.gender" placeholder="请输入性别" size="small"></el-input>
        </el-form-item>
        <el-form-item label="工号" prop="workId"
          :rules="[
            {required: true, message: '请输入工号'}
          ]">
          <el-input class="input-box" v-model="operator.workId" placeholder="请输入工号信息" size="small"></el-input>
        </el-form-item>
        <div>
          <el-button type="plain" size="small" @click="closeDialog">取消</el-button>
          <el-button type="primary" size="small" @click="createProject">添加</el-button>
        </div>
      </el-form>
  </el-dialog>
</template>

<script>
export default {
  name: 'AddProject',
  props: {
    isShow: {
      type: Boolean,
      default: () => Boolean
    }
  },
  data () {
    return {
      operator: {
        name: '',
        gender: '',
        workId: ''
      }
    }
  },

  methods: {
    closeDialog () {
      this.$emit('closeDialog', 'showAddOperator')
    },

    // 提交项目信息
    createProject () {
      this.$refs.form.validate(async (a, b) => {
        if (a) {
          const options = this.operator
          const res = await this.$store.dispatch('createOperator', options)
          console.log(res, 'res')
          this.closeDialog()
          if (res.success) {
            this.$message({
              type: 'success',
              message: '操作员信息创建成功',
              showClose: true
            })
          } else {
            this.$message({
              type: 'info',
              message: '操作员信息创建失败,请重新添加',
              showClose: true
            })
          }
        }
      })
    }
  }
}
</script>

<style>
.el-form {
  margin: 0 auto;
  width: 400px;
}
.input-box {
  width: 300px;
}
</style>
